import * as THREE from "three";

const FenceLinesShader = {
  shaderID: 'FenceLinesShader',

  uniforms: {
    'iTime': { value: 1.0 },
    'height': { value: new THREE.Vector2(0,1) },
    'fenceColor': { value: new THREE.Color() },
    'period': {value: 0.45}, // 周期越小越密集，最好不要小于0.01
  },

  vertexShader: /* glsl */`
  varying float zVal;
  uniform vec2 height;

  void main() {
    zVal = position.z - height.x;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  }`,

  fragmentShader: /* glsl */`
	uniform float iTime;
  uniform vec2 height;
  uniform float period;
  uniform vec3 fenceColor;
  varying float zVal;
  
  void main() {
    float h = height.y - height.x;
    float y = mod(zVal - iTime, h);
    float b = smoothstep(0.01, period - 0.01, mod(y, period));
  
    vec4 colorFence = vec4(fenceColor, 1.0);
    vec4 colorWhite = vec4(1.0, 1.0, 1.0, 0.0);
    float k = abs(0.5 - b) * 2.0;
    gl_FragColor = mix(colorFence, colorWhite, k);
  }
`
}

export default FenceLinesShader;
